/*
 *
 *  *****************************************************************************
 *  * Copyright ( c ) 2016 Heren Tianjin Inc. All Rights Reserved.
 *  *
 *  * This software is the confidential and proprietary information of Heren Tianjin Inc
 *  * ("Confidential Information").  You shall not disclose such Confidential Information
 *  *  and shall use it only in accordance with the terms of the license agreement
 *  *  you entered into with Heren Tianjin or a Heren Tianjin authorized
 *  *  reseller (the "License Agreement").
 *  ****************************************************************************
 *  *
 */

package com.herench.bunch.commons.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by luoxiaoming on 16-8-3.
 */
public class DataFormUtils {

    private static final String DATE_TIME_PATTERN ="yyyy-MM-dd HH:mm:ss";

    private static final String DATE_PATTERN ="yyyy-MM-dd";


    public static final SimpleDateFormat DATE_TIME_FORMAT = new SimpleDateFormat();
    public static DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");


    public static String dateTimeFormat(Date date,String ...format){
        if(format!=null && format.length>0){
            DATE_TIME_FORMAT.applyPattern(format[0]);
        }else {
            DATE_TIME_FORMAT.applyPattern(DATE_TIME_PATTERN);
        }
        return DATE_TIME_FORMAT.format(date);
    }

    public static String nows() {
        return dateTimeFormat(new Date());
    }



    public static Date parse(String timeString,String ...format) throws ParseException {
        if(format!=null && format.length>0){
            DATE_TIME_FORMAT.applyPattern(format[0]);
        }else {
            DATE_TIME_FORMAT.applyPattern(DATE_TIME_PATTERN);
        }
        return DATE_TIME_FORMAT.parse(timeString);
    }

    public static String getCurrentDate(DateTimeFormatter formatter) {
        LocalDateTime dateTime = LocalDateTime.now();
        return dateTime.format(formatter);
    }


    public static Map<String,Object> getAgeFromBirthday(Date birthday) throws Exception{
        Map<String,Object> res = new HashMap<>();

        Calendar now = Calendar.getInstance();
        Calendar birth = Calendar.getInstance();
        birth.setTime(birthday);

        int year = now.get(Calendar.YEAR) - birth.get(Calendar.YEAR);
        int month =  now.get(Calendar.MONTH) - birth.get(Calendar.MONTH);
        int day =  now.get(Calendar.DAY_OF_YEAR) - birth.get(Calendar.DAY_OF_YEAR);

        if (year>0){
            res.put("age",year+"");
            res.put("ageUnit","岁");
            return res;
        }else if (month>0){
            res.put("age",month+"");
            res.put("ageUnit","月");
            return res;
        }else if (day>0){
            res.put("age",day+"");
            res.put("ageUnit","天");
            return res;
        }
        return res;
    }

    public static Map<String,Object> getAgeFromBirthday(String date) throws Exception{
        Date birthday = parse(date,DATE_PATTERN);
        return getAgeFromBirthday(birthday);
    }

}
